package com.zzw.statelayoutlib.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.zzw.statelayoutlib.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ZTitleView extends View {

    /**
     * 文本
     */
    private String mTitleText;
    /**
     * 文本的颜色
     */
    private int mTitleTextColor;
    /**
     * 文本的大小
     */
    private int mTitleTextSize;
    /**
     * 图片
     */
    private Bitmap mTitleImg;
    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mBound;
    Paint paint;

    public ZTitleView(@NonNull Context context) {
        this(context, null);
    }

    public ZTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZTitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZTitleView, defStyleAttr, 0);
        int current = a.getIndexCount();
        for (int i = 0; i < current; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.ZTitleView_titlebackImg) {
                mTitleImg = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
            } else if (attr == R.styleable.ZTitleView_titleText) {
                mTitleText = a.getString(attr);
            } else if (attr == R.styleable.ZTitleView_titleTextColor) {
                mTitleTextColor = a.getColor(attr, Color.BLACK);
            } else if (attr == R.styleable.ZTitleView_titleTextSize) {
                //mTitleTextSize=typedArray.getDimensionPixelSize(attr,16);
                mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
            }
        }
        a.recycle();
        paint = new Paint();
        paint.setTextSize(mTitleTextSize);
        mBound = new Rect();
        paint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(getRealWidth(widthMode, width), getRealHeight(heightMode, height));

    }

    private int getRealWidth(int mode, int current) {

        if (mode == MeasureSpec.AT_MOST) {//子布局限制在一个最大值内
            current = getPaddingLeft() + mBound.width() + getPaddingRight();
        } else if (mode == MeasureSpec.EXACTLY) {//设置了明确的值或者是MATCH_PARENT

        } else if (mode == MeasureSpec.UNSPECIFIED) {//子布局想要多大就多大，很少使用

        }
        return current;
    }

    private int getRealHeight(int mode, int current) {
        if (mode == MeasureSpec.AT_MOST) {//子布局限制在一个最大值内
            current = getPaddingTop() + mBound.height() + getPaddingBottom();
        } else if (mode == MeasureSpec.EXACTLY) {//设置了明确的值或者是MATCH_PARENT

        } else if (mode == MeasureSpec.UNSPECIFIED) {//子布局想要多大就多大，很少使用

        }
        return current;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mTitleImg, 0, 0, paint);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
        paint.setColor(mTitleTextColor);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, paint);

    }
}
